<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if(!isset($project) && !($project instanceof LM_Table_Project)){
	LM::redirect();
}

if(!$project->hasRounds()){
	LM::redirect("rounds", "Create some Rounds before entering the result manager!");
}

switch($task){
	
	case 'quickadd':
		$team1	= intval(mosGetParam($_POST, "team1", 0));
		$team2	= intval(mosGetParam($_POST, "team2", 0));
		
		$rid	= intval(LM::getSessionValueFromRequest("_filterresultsbyround_", "rid", $project->getCurrentRound()));
		$group	= 		 LM::getSessionValueFromRequest("_filterbygroup_", "group", "");
		
		if($rid > 0 && $team1 > 0 && $team2 > 0){
			
			$projectmanager = new Projectmanager($project);
			
			if($projectmanager->createMatch($team1, $team2, $rid, null, null, $group)){
				LM::redirect("matches", "Match successfully created");
			} else {
				LM::redirect("matches", "Could not create Match");
			}
		} else{
			LM::redirect("matches", "Round MUST NOT be 0, also choose teams, if not!");
		}
	break;
	
	case 'events':
	case 'lineup':
		LM::setMatch($cid[0]);
		LM::unsetSessionValue("_filterbylineupteam_");
		LM::redirect($task);
	break;
	
	case 'setcurrent':
		$rid	= intval(LM::getSessionValueFromRequest("_filterresultsbyround_", "rid", $project->project_round));
		$project->project_cur_round = $rid;
		$project->store();
		LM::redirect("matches");
	break;

	case 'edit':
	
		$row = LM::getLMTable("Match");
		$row->load( $cid[0] );
		
		$team1 = $row->getTeam1();
		$team2 = $row->getTeam2();
		
		$sql = "SELECT player_id AS value, player_name AS text FROM #__leagueman_player WHERE player_is_referee = 1";
		$database->setQuery($sql);
		$referees = $database->loadObjectList();
		
		$template = LM::getTemplate();
		
		$template->assign("view",			"admin/form_matches.tpl.php");
		$template->assign('project',		$project);
		$template->assign('team',			$team);
		$template->assign('my',				$my);
		$template->assign('row',			$row);
		$template->assign('team1',			$team1);
		$template->assign('team2',			$team2);
		$template->assign('referees',		$referees);
		$template->display("admin/view.tpl.php");
	break;
	
	case 'saveresults':
		
		$total = count($_POST["cid"]);
		
		$date = "0000-00-00";
		$time = "00:00";
		
		for($i = 0; $i < $total; $i++){
			
			$row = LM::getLMTable("Match");
			
			$mid = intval($_POST["cid"][$i]);
			
			if($row->load($mid)){
				
				$date_tmp = isset($_POST["match_date"][$i]) && isDate($_POST["match_date"][$i]) ? $_POST["match_date"][$i] : "0000-00-00";
				$time_tmp = isset($_POST["match_time"][$i]) && isTime($_POST["match_time"][$i]) ? $_POST["match_time"][$i] : "00:00";
			
				if(strcmp($date, $date_tmp) && $date_tmp != "0000-00-00"){
					$date = $date_tmp;
				}
			
				if(strcmp($date, $time_tmp) && $time_tmp != "00:00"){
					$time = $time_tmp;
				}
				
				$row->match_date	= $date;
				$row->match_time	= $time;
				
				if($project->getParam("groups_supported", 0) && isset($_POST["match_group"][$i])){
					$row->match_group	= $_POST["match_group"][$i];
				}
			
				if($project->getParam("allow_extra_time", 0) && isset($_POST["match_result_type"][$i])){
					$row->match_result_type = intval($_POST["match_result_type"][$i]);
				}
				
				for($j = 0; $j <= 5; $j++){
				
					$v1 = "match_result_{$j}_1";
					$v2 = "match_result_{$j}_2";
					
					$e1 = isset($_POST[$v1][$i]) ? $_POST[$v1][$i] : null;
					$e2 = isset($_POST[$v2][$i]) ? $_POST[$v2][$i] : null;
					
					if(is_numeric($e1) && is_numeric($e2)){
						$row->$v1 = $e1;
						$row->$v2 = $e2;
					}
				}
				
				$row->store();
			}
		}
		
		LM::redirect("matches");
	break;
	
	case 'prev':
	case 'next':
	case 'save':
		$row = LM::getLMTable("Match");
		$row->save($_POST, "");
		
		switch($task){
			case 'prev':
				mosRedirect("index2.php?option=com_leagueman&act=matches&task=edit&cid=");
			break;
			
			case 'next':
				mosRedirect("index2.php?option=com_leagueman&act=matches&task=edit&cid=");
			break;
			
			default:
				LM::redirect("matches");
			break;
		}
	break;
	
	case 'cancel':
		LM::redirect("matches");
	break;
	
	case 'setInfo':
		$row = LM::getLMTable("Match");
		$row->load($cid[0]);
		$row->match_info_flag = 1;
		$row->store();
		
		LM::redirect("matches");
	break;
	
	case 'unsetInfo':
		$row = LM::getLMTable("Match");
		$row->load($cid[0]);
		$row->match_info_flag = 0;
		$row->store();
		LM::redirect("matches");
	break;
	
	case 'publish':
		$row = LM::getLMTable("Match");
		$row->publish($cid, 1, $my->id);
		LM::redirect("matches");
	break;
	
	case 'unpublish';
		$row = LM::getLMTable("Match");
		$row->publish($cid, 0, $my->id);
		LM::redirect("matches");
	break;
	
	case 'remove':
		
		$projectmanager = new Projectmanager($project);
		
		$total = count($cid);
		
		$i = 0;
		foreach($cid AS $id){
			if($projectmanager->deleteMatch($id)){
				$i++;
			}
		}
		
		LM::redirect("matches", $i . " from " . $total . " Results deleted");
	break;
	
	default:
		$defaults["tid"]	= intval(	LM::getSessionValueFromRequest("_filterbyteam_", 			"tid", 0));
		$defaults["rid"]	= intval(	LM::getSessionValueFromRequest("_filterresultsbyround_",	"rid", $project->getCurrentRound()));
		$defaults["mode"]	= intval(	LM::getSessionValueFromRequest("_filterbymode_",			"mode", 0));
		$defaults["group"]	= 			LM::getSessionValueFromRequest("_filterbygroup_",			"group", "");
		
		$projectmanager = new Projectmanager($project);
		
		$rounds[] = mosHTML::makeOption('0', 'Round');
		$rounds = array_merge($rounds, $projectmanager->getRoundBox());

		$teams[] = mosHTML::makeOption( '0', 'Team' );
		$teams = array_merge( $teams, $project->getTeamBox());
		
		$modes[] = mosHTML::makeOption( '0', 'Mode');
		$modes[] = mosHTML::makeOption( '1', 'Home');
		$modes[] = mosHTML::makeOption( '2', 'Away');
					
		$groups[] = mosHTML::makeOption("", "Group");
		$groups = array_merge($groups, $project->getGroupBox());
		
		$rows = $projectmanager->getMatches($defaults["rid"], $defaults["tid"], $defaults["mode"], $defaults["group"]);
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/list_matches.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("rounds",		$rounds);
		$template->assign("teams",		$teams);
		$template->assign("modes",		$modes);
		$template->assign("groups",		$groups);
		$template->assign("defaults",	$defaults);
		$template->assign("rows", 		$rows);
		$template->assign("my",			$my);
		
		$template->display("admin/view.tpl.php");
	break;
}

?>